home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / OTFLIP.ZIP / pageflip.doc < prev    next >
Text File  |  1996-02-23  |  7KB  |  155 lines

  1.  
  2.                       - THE OUTLAW TRIAD DEMO-SERIES -
  3.  
  4. ────────────────────────────────■ PART III ■──────────────────────────────────
  5.  
  6.                           Written by : Vulture/OT
  7.                           Code in    : Pascal/Asm
  8.                           Topic      : Virtual screens
  9.  
  10. ──────────────────────────────■ Introduction ■───────────────────────────────
  11.  
  12.  Welcome to the Outlaw Triad demo-series! In these series we will be talking
  13.  about programming demo-effects in either pascal or assembler. Theory behind
  14.  the effects shall be discussed while a full sourcecode is also provided.
  15.  In this latest release we will be talking about the use of the socalled
  16.  virtual screens. These are very powerfull tools when coding demos or any
  17.  other graphic related program. A full sourcecode in Turbo Pascal is provided
  18.  in this package. Enjoy!
  19.  
  20. ─────────────────────────────────■ Theory ■──────────────────────────────────
  21.  
  22.  What exactly is a virtual screen? Well, a virtual screen is nothing more
  23.  than a piece of base-memory representing the VGA screen. So, when using
  24.  standard mode 13h, the virtual screen is 64000 bytes large. Because the
  25.  vga is 320*200=64000 bytes.
  26.  
  27.  Now what can you do with a virtual screen? A major advantage of using these
  28.  virtual screens is that you can avoid bad flickering on the screen. The plan
  29.  is to update the data in memory and then flip it to the VGA. Updating the
  30.  VGA memory directly can cause a lot of "snow" on the screen. Suppose you are
  31.  writing a demo in which a lot of sprites and other things are moving on the
  32.  screen. But what happens if you want to have a picture in the background?
  33.  The picture would be erased because the sprites are drawn on top of it.
  34.  That's no good! Here we use a virtual screen.
  35.  You could do something like this:
  36.  
  37.     1. - Draw the picture on the virtual screen.
  38.     2. - Flip (copy) the picture to the vga (old frames are erased).
  39.     3. - Wait for a vertical retrace (to make it even smoother).
  40.     4. - Draw your sprites on the vga (next frames).
  41.     5. - Back to 2.
  42.  
  43.  So you see? This way your picture won't be erased at all! You constantly
  44.  copy your virtual page to the VGA before you write the next frame. You
  45.  might say that this is a rather slow way of doing things. This is true.
  46.  But better use slow tricks than having bad screen updates, right?
  47.  You should write your pageflipping routine(s) in assembler. Don't worry,
  48.  for those of you with little experience in coding asm, I have included
  49.  my own assembler routines. Those are pretty fast (using extended registers
  50.  for fast copying).
  51.  
  52.  Anyway, how do we setup a virtual screen? Our source is in Pascal. Now,
  53.  in Pascal you may not have a single variable of 64000 bytes. That would
  54.  be too large. The solution is using pointers. Do it like this:
  55.  
  56.  Type Virtual  = Array[1..64000] of byte;  { The size of our Virtual Screen }
  57.       VirPoint = ^Virtual;                 { Pointer to the virtual screen }
  58.  
  59.  Var Virscr: VirPoint;                     { The actual virtual screen }
  60.      Vaddr : Word;                         { Segment of our virtual screen }
  61.  
  62.  Procedure SetMemory;                      { Allocates memory }
  63.  Begin
  64.    GetMem(VirScr,64000);                   { 64000 bytes }
  65.    Vaddr := Seg(Virscr^);
  66.  End;
  67.  
  68.  Procedure FreeMemory;                     { Frees the memory }
  69.  Begin
  70.    FreeMem(VirScr,64000);
  71.  End;
  72.  
  73.  Ok, when you use "SetMemory" inside your program, it would setup 64000
  74.  bytes of base-memory for you to use and "Vaddr" would be pointing to the
  75.  segment of this memory (virtual screen). When your program is done, you
  76.  use "FreeMemory" to... well... free the memory. If you don't do that,
  77.  programs you run after you executed your own program have 64000 bytes
  78.  less to work with and that's no good. Now, right after you have setup
  79.  your virtual page, you must clear it from any garbage that might still
  80.  exist there. You can do that by writing 64000 zeros to that memory.
  81.  Examine the source to see what I mean.
  82.  
  83.  All is fine sofar, but how do we actually access the virtual page?
  84.  Let me give you an example by plotting a pixel on the virtual page with
  85.  the "SetPixel" procedure. Here's the procedure in assembler:
  86.  
  87.  Procedure SetPixel(X,Y:Integer;Color:Byte;Where:Word); Assembler;
  88.  Asm
  89.    mov  ax,[Where]
  90.    mov  es,ax                  { es:di => destination }
  91.    mov  di,Y
  92.    mov  ax,Y
  93.    shl  di,8
  94.    shl  ax,6
  95.    add  di,ax                  { di = y*320 }
  96.    mov  ax,X
  97.    add  di,ax                  { di = (y*320)+x }
  98.    mov  al,Color
  99.    mov  byte ptr es:[di],al    { Place dot }
  100.  End;
  101.  
  102.  Let's say you want to have a pixel right in the middle of the virtual
  103.  screen. The exact place is calculated in the same manner as on the VGA,
  104.  that is (Y*320)+X. Why? Because base-memory, like VGA-memory, is lineair!
  105.  So, the middle of the screen would be X=160, Y=100. Let's say the color
  106.  is 1. Your total command would now be: SetPixel(160,100,1,Vaddr).
  107.  And that's it! Instead of pointing to the VGA's segment, you point to
  108.  the segment of the virtual screen. As you see it's very simple indeed.
  109.  
  110.  Using base-memory is not the only way of using virtual screens. You can
  111.  also use plain VGA-memory for it. But in order to do that, you must use
  112.  an unchained VGA-mode. E.g, Mode Y has a resolution of 320*200*256 and
  113.  has got 4 pages (screens) available in display(vga) memory.
  114.  Read "chain4.doc" and "tweakdoc.txt" for more info on that. These are
  115.  textfiles written by me (Vulture/OT) on unchaining and tweaking the VGA.
  116.  
  117.  Ok, now examine the source to see a quick example of virtual screens.
  118.  In the sample program I setup a virtual screen, fill it with random
  119.  pixels and wait for a keypress. Then I flip all data on the virtual
  120.  screen to the VGA. It's a short program but I am sure you can now see
  121.  the use and power of virtual screens. Btw, the assembler procedures
  122.  found here are very fast. Only thing I ask in return is that you greet
  123.  me or Outlaw Triad in your productions. Is that cheap or what? ;-)
  124.  In the future I might release a pageflipping program in 100% assembler.
  125.  
  126.  Ok, this is all for now. Happy coding!
  127.  
  128.        - Vulture / Outlaw Triad -
  129.  
  130. ───────────────────────────────■ Distro Sites ■──────────────────────────────
  131.  
  132.  Call our distros to get all our releases.
  133.  
  134.   BlueNose    World HQ          +31 (0)345-619401
  135.   FireHouse   Distrosite        +31 (0)528-274176
  136.   The Force   Distrosite        +31 (0)36-5346967    More distros wanted!
  137.   MagicWare   Italian HQ        +39  6-52355532
  138.   ShockWave   South African HQ  +27 (011)888-6345
  139.  
  140. ──────────────────────────────────■ Contact ■────────────────────────────────
  141.  
  142.  Want to contact Outlaw Triad for some reason? You can reach us at our
  143.  distrosites in Holland. Or if you have e-mail access, mail us:
  144.  
  145.    Vulture  (coder/pr)     comma400@tem.nhl.nl
  146.  
  147.  Our internet homepage:
  148.  
  149.    http://www.tem.nhl.nl/~comma400/vulture.html
  150.  
  151.  These internet adresses should be valid at least till june 1996.
  152.  
  153. ──────────────────────────────────────────────────────────────────────────────
  154.  
  155.             Quote: Black holes are there where God divided by zero.